package com.jzmy.cultivate.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.jzmy.cultivate.dao.ConsultationsDao;
import com.jzmy.cultivate.entity.Consultations;
import com.jzmy.cultivate.entity.vo.ConsultationsVo;
import com.jzmy.cultivate.service.ConsultationsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mengzhichao
 * @create 2022-08-25-9:31
 */
@Service
public class ConsultationsServiceImpl extends ServiceImpl<ConsultationsDao, Consultations> implements ConsultationsService {
    @Override
    public Page<ConsultationsVo> selectListPage(int current, int number, String firstColumn, String columnName,String title) {
        // 新建分页
        Page<ConsultationsVo> page = new Page<ConsultationsVo>(current, number);
        if("职业技能等级标准".equals(columnName)){
            List<ConsultationsVo> list = baseMapper.dyGetUserList5(page, firstColumn, columnName);
//            list = listPdfGSH(list);
            return page.setRecords(list);
        }
        //定义key
        // 根据情况，返回分页结果
        if(StringUtils.isNotBlank(columnName)){
            List<ConsultationsVo> list=this.baseMapper.dyGetUserList2(page,firstColumn,columnName);
            list = listPdfGSH(list);
            return page.setRecords(list);
        }else{
            if(StringUtils.isNotBlank(firstColumn)){
                List<ConsultationsVo> list=this.baseMapper.dyGetUserList3(page,firstColumn);
                listPdfGSH(list);
                return page.setRecords(list);
            }else{
                if (StringUtils.isNotBlank(title)) {
                    List<ConsultationsVo> list = this.baseMapper.dyGetUserList4(page,title);
                    listPdfGSH(list);
                    return page.setRecords(list);
                }else {
                    List<ConsultationsVo> list = this.baseMapper.dyGetUserList1(page);
                    listPdfGSH(list);
                    return page.setRecords(list);
                }
            }
        }
    }



    private List<ConsultationsVo> listPdfGSH(List<ConsultationsVo> list){
        List<ConsultationsVo> rst =new ArrayList<>();
        for (int i=0;i<list.size();i++){
            if (isPdf(list.get(i).getFiles())){
                rst.add(list.get(i));
            }
        }
        return rst;
    }


    private static Boolean isPdf(String files){
        String ext = files.substring(files.lastIndexOf(".") + 1);
        if ("pdf".equals(ext)){
            return true;
        }
        if ("rar".equals(ext)){
            return true;
        }
        if ("zip".equals(ext)){
            return true;
        }
        return false;
    }


}
